package com.cali.libcore.dialog

import android.app.Activity
import android.graphics.Color
import android.graphics.Point
import android.graphics.drawable.ColorDrawable
import android.os.Build
import android.os.Bundle
import android.view.*
import androidx.fragment.app.DialogFragment
import com.cali.libcore.dialog.manager.MyDialogsManager


/**
 * Created by mq on 2018/8/31 上午11:39
 * mqcoder90@gmail.com
 */

abstract class MyBaseDialog : DialogFragment() {
    protected var baseView: View? = null
        private set

    protected abstract val layoutRes: Int

    protected abstract val dialogView: View?

    private val isCancelableOutside: Boolean
        get() = true

    private val dialogWidth: Int
        get() = WindowManager.LayoutParams.WRAP_CONTENT

    private val dialogHeight: Int
        get() = WindowManager.LayoutParams.WRAP_CONTENT

    private val dimAmount: Float
        get() = 0.2f

    protected val gravity: Int
        get() = Gravity.CENTER

    private val animRes: Int
        get() = 0

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {

        if (layoutRes > 0) {
            //调用方通过xml获取view
            baseView = inflater.inflate(layoutRes, container, false)
        } else if (dialogView != null) {
            //调用方直接传入view
            baseView = dialogView
        }
        return baseView
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        val dialog = dialog
        if (dialog != null) {
            dialog.requestWindowFeature(Window.FEATURE_NO_TITLE)
            //如果isCancelable()是false 则会屏蔽物理返回键
            dialog.setCancelable(isCancelable)
            //如果isCancelableOutside()为false 点击屏幕外Dialog不会消失；反之会消失
            dialog.setCanceledOnTouchOutside(isCancelableOutside)
            //如果isCancelable()设置的是false 会屏蔽物理返回键
            dialog.setOnKeyListener { dialog, keyCode, event -> keyCode == KeyEvent.KEYCODE_BACK && event.action == KeyEvent.ACTION_DOWN && !isCancelable }
        }
    }

    override fun onStart() {
        super.onStart()
        val dialog = dialog ?: return
        val window = dialog.window ?: return
//设置背景色透明
        window.setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT))
        //设置Dialog动画效果
        if (animRes > 0) {
            window.setWindowAnimations(animRes)
        }
        val params = window.attributes
        //设置Dialog的Width
        if (dialogWidth > 0) {
            params.width = dialogWidth
        } else {
            params.width = WindowManager.LayoutParams.WRAP_CONTENT
        }
        //设置Dialog的Height
        if (dialogHeight > 0) {
            params.height = dialogHeight
        } else {
            params.height = WindowManager.LayoutParams.WRAP_CONTENT
        }
        //设置屏幕透明度 0.0f~1.0f(完全透明~完全不透明)
        params.dimAmount = dimAmount
        params.gravity = gravity
        window.attributes = params
    }

    override fun onDestroy() {
        super.onDestroy()
        MyDialogsManager.instance.over()
    }

    companion object {

        private val point = Point()


        /**
         * 获取屏幕宽度
         *
         * @param activity Activity
         * @return ScreenWidth
         */
        fun getScreenWidth(activity: Activity): Int {
            val display = activity.windowManager.defaultDisplay
            if (display != null) {
                display.getSize(point)
                return point.x
            }
            return 0
        }

        /**
         * 获取屏幕高度
         *
         * @param activity Activity
         * @return ScreenHeight
         */
        fun getScreenHeight(activity: Activity): Int {
            val display = activity.windowManager.defaultDisplay
            if (display != null) {
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
                    //api 大于17才有
                    display.getRealSize(point)
                } else {
                    display.getSize(point)
                }

                //需要减去statusBar的高度  不用考虑navigationBar Display已经自动减去了
                return point.y - getStatusBarHeight(activity)
            }
            return 0
        }

        private fun getStatusBarHeight(activity: Activity): Int {
            val resources = activity.resources
            val resourceId = resources.getIdentifier("status_bar_height", "dimen", "android")
            return resources.getDimensionPixelSize(resourceId)
        }

        fun getNavigationBarHeight(activity: Activity): Int {
            val resources = activity.resources
            val resourceId = resources.getIdentifier("navigation_bar_height", "dimen", "android")
            return resources.getDimensionPixelSize(resourceId)
        }
    }
}
